package cn.xorange.commons.core.auth.handler;

import cn.xorange.commons.core.auth.service.UserPermissionService;
import cn.xorange.commons.security.configuration.SecurityProperties;
import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.enums.LoginType;
import cn.xorange.commons.security.enums.SysType;
import cn.xorange.commons.security.exception.SecurityException;
import cn.xorange.commons.security.mapper.LoginUserMapper;
import cn.xorange.commons.security.service.LoginHandler;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.core.constants.SysConstants;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : yangjian
 * &#064;date  : 2023/12/18
 * &#064;description  : 后台账号密码登录处理类
 */

public class SysPwLoginHandler implements LoginHandler {

    @Autowired
    LoginUserMapper userMapper;
    @Resource
    SecurityProperties securityProperties;
    @Autowired
    UserPermissionService permissionService;


    @Override
    public String getLoginSysAndType() {
        return this.config().getSysType() + ":" + config().getLoginType();
    }

    @Override
    public SecurityProperties config() {
        SecurityProperties properties = new SecurityProperties();
        properties.setSysType(SysType.SYS.type());
        properties.setLoginType(LoginType.PASSWORD.type());
        properties.setEnableCaptcha(true);
        properties.setCaptchaExpire(3 * 60L);
        properties.setTokenExpire(2 * 60 * 60L);
        return properties;
    }

    @Override
    public SecurityUser handle(JSONObject object) {
        String loginName = object.getString(SecurityConstants.LOGIN_NAME);
        String pwd = object.getString(SecurityConstants.LOGIN_PASSWORD);
        Long tenantId = object.getLong(SecurityConstants.USER_TENANT_ID);
        String isSupper = object.getString(SecurityConstants.USER_IS_SUPPER);

        SecurityUser user = this.userMapper.selectSysUserDetail(loginName, tenantId, securityProperties.isEnableTenant(), isSupper);
        if(Objects.isNull(user)){
            throw SecurityException.userOrPwdError();
        }
        boolean checkPassword = SecurityUtils.matchesPassword(pwd,user.getPassword());
        if(Boolean.FALSE.equals(checkPassword)){
            throw SecurityException.userOrPwdError();
        }
        user.setTenantId(tenantId);
        user.setEnable(SysConstants.FLAG_SUCCESS.equals(user.getStatus()));
        if (SysConstants.FLAG_YES.equals(isSupper)){
            user.setIsAdmin(SysConstants.FLAG_YES);
        }
        this.permissionService.setTenantOwnerAdmin(user);
        this.permissionService.setUserRoles(user);
        this.permissionService.setUserRoleKeys(user);
        this.permissionService.setUserPermissionKeys(user);
        return user;
    }


}
